package com.uchedao.vehicledetection.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchedao.mybaselibrary.utils.ImageUtils;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.utils.Util;

public class UploadPhotoAdapter extends BaseAdapter {

    public interface UploadPhotoListener {
        /**
         * 添加图片
         *
         * @param position
         */
        public void add(int position);

        /**
         * 删除图片
         *
         * @param position
         */
        public void del(int position);
    }

    private String[] array;
    private String[] picsPath;// 缩略图路径
    private LayoutInflater inflater;

    private UploadPhotoListener listener;

    private Context mContext;

    public UploadPhotoAdapter(Context context, String[] texts, String[] pics, UploadPhotoListener uploadPhotoListener) {
        array = texts;
        mContext = context;
        picsPath = pics;
        listener = uploadPhotoListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (array == null)
            return 0;
        return array.length;
    }

    /**
     * 设置图片
     *
     * @param position
     * @param path
     */
    public void setPic(int position, String path) {
        picsPath[position] = path;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return array[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        ImageView img;
        TextView delete;
        TextView name;
    }

    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            switch (v.getId()) {

                case R.id.upload_photo_iv:// 选择图片
                    if (listener != null)
                        listener.add(position);
                    break;

                case R.id.upload_photo_delete_tv:// 删除图片
                    // picsPath[position] = "";
                    // notifyDataSetChanged();
                    if (listener != null)
                        listener.del(position);
                    break;
            }
        }
    };

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_upload_photo, null);
            convertView.setTag(viewHolder);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.upload_photo_iv);
            viewHolder.delete = (TextView) convertView.findViewById(R.id.upload_photo_delete_tv);
            viewHolder.name = (TextView) convertView.findViewById(R.id.upload_photo_tv);

            viewHolder.img.setOnClickListener(onClickListener);
            viewHolder.delete.setOnClickListener(onClickListener);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.img.setTag(position);
        viewHolder.delete.setTag(position);

        if (TextUtils.isEmpty(picsPath[position])) {
            viewHolder.img.setImageBitmap(null);
            viewHolder.img.setBackgroundResource(R.drawable.add_photo_bg);
            viewHolder.delete.setVisibility(View.INVISIBLE);

        } else {
//			Bitmap bitmap = getBitmap(picsPath[position]);
//			if (bitmap != null)
//				viewHolder.img.setImageBitmap(bitmap);
//			else
//				viewHolder.img.setBackgroundResource(R.drawable.add_photo_bg);
//			// viewHolder.img.setBackgroundDrawable(BitmapDrawable
//			// .createFromPath(picsPath[position]));
            ImageUtils.loadImage("file:///" + picsPath[position], viewHolder.img);
            viewHolder.delete.setVisibility(View.VISIBLE);
        }

        // 设置是否显示星号
        setStar(viewHolder.name, position == 0 && TextUtils.isEmpty(picsPath[position]));

        viewHolder.name.setText(array[position]);
        return convertView;
    }

    /**
     * 设置是否显示星号
     *
     * @param tv
     * @param show
     */
    void setStar(TextView tv, boolean show) {
        tv.setCompoundDrawablesWithIntrinsicBounds(show ? R.mipmap.read_star : R.mipmap.read_star_no, 0, 0, 0);
    }

    private Bitmap getBitmap(String path) {
        if (TextUtils.isEmpty(path))
            return null;
        return Util.getimage(path, 329, 249);
    }
}
